<?php

class UserStats{
        function getRank($steamID){

                if($steamID > 0){
                        $DB = new DB();
                        $con = $DB->conDB();
                        $sql = "SELECT Rank
                                        FROM UserStats
                                        WHERE SteamID = ".$steamID."
                                                        ";
                        $ret = $DB->select($sql);

                        return $ret["Rank"];
                }
                else{
                        return 0;
                }
        }

        function updateUserStatsData($steamID){
                $DB = new DB();
                $con = $DB->conDB();

                $DotaBuff = new DotaBuff();
                $data = $DotaBuff->getStats($steamID);

                $sql = 'UPDATE UserStats
                                SET WinLoseRatio="'.$data["ratio"].'", Lvl="'.$data["lvl"].'", Wins="'.$data["wins"].'", Loses="'.$data["loses"].'"
                                                WHERE SteamID = '.$steamID.'
                                                                ';
                $DB->update($sql, 0);
        }

        function checkUserHaveStats($steamID){
                $DB = new DB();
                $con = $DB->conDB();
                $sql = "SELECT DISTINCT SteamID
                                FROM UserStats
                                WHERE SteamID = ".$steamID."
                                                ";
                $count = $DB->countRows($sql,0);
                //p($count);
                if($count > 0){
                        return true;
                }
                else{
                        return false;
                }
        }

        function getUserStats($steamID, $select="*"){
                $ret = array();
                if($steamID > 0){
                        $DB = new DB();
                        $con = $DB->conDB();

                        $sql = "SELECT DISTINCT ".$select."
                                        FROM UserStats
                                        WHERE SteamID = ".secureNumber($steamID)."
                                                        ";
                        $data = $DB->select($sql);
                        $ret['debug'] = $sql;
                        $ret['data'] = $data;
                }
                return $ret;
        }

        function updateGeneralUserStats($steamID, $result, $matchID){
                $ret = array();
                if($steamID > 0 && $matchID > 0){
                        $DB = new DB();
                        $con = $DB->conDB();
                        $Match = new Match();
                        
                        $leaver = $Match->playerLeftTheMatch($steamID, $matchID);
                        $playersLeftMatch = $Match->getPlayersWhoLeftTheMatch($matchID);
                        $countPlayersLeft = (int) count($playersLeftMatch['data']);
                        
                        switch($result){
                                // WIN
                                case 1:
                                        $set = "SET Wins = Wins+1
                                                        ";
                                        break;
                                        // LOSE
                                case -1:
                                        if($countPlayersLeft === 0){
                                                $set = "SET Loses = Loses+1
                                                        ";
                                        }
                                        break;
                                default: $set = "SET Wins = Wins";
                        }

                        // Leaver handling
                        if($leaver['left']){
                                if($set != ""){
                                        $set .= ", Leaves = Leaves+1";
                                }
                                else{
                                        $set = "SET Leaves = Leaves+1";
                                }
                        }
                        // neue WinRate updaten
                        if($set != ""){
                        	$set .= ", WinLoseRatio = (Wins/(Wins+Loses))*100";
                        	
                        	$sql = "UPDATE UserStats
                                        ".$set."
                                                        WHERE SteamID = ".secureNumber($steamID)."
                                                                        ";
                        	$DB->update($sql);
                        	$ret['debug'] = $sql;
                        	$ret['status'] = true;
                        }
                        else{
                        	$ret['status'] = false;
                        }

                       
                       
                }
                return $ret;

        }

        function updateRank($steamID, $rank){
                $ret = array();
                $DB = new DB();
                $con = $DB->conDB();

                if($steamID > 0){
                        $sql = "UPDATE `UserStats`
                                        SET Rank = ".(int) $rank."
                                                        WHERE SteamID = ".secureNumber($steamID)."";
                        $DB->update($sql);
                        $ret['status'] = true;
                }
                else{
                        $ret['status'] = "SteamID 0";
                }
                return $ret;
        }

        function getWallOfFameData(){
                $ret = array();
                $DB = new DB();
                $con = $DB->conDB();

                // Diamond
                $sql = "SELECT *
                                FROM UserStats us JOIN User u ON u.SteamID = us.SteamID
                                WHERE Rank >= ".DIAMONDELO."
                                                ORDER BY Rank DESC
                                                LIMIT 1
                                                ";
                $ret['debug'] .= p($sql,1);
                $diamondData = $DB->multiSelect($sql);
                if(count($diamondData) > 0){
                        $ret['data']['diamond'] = $diamondData[0];
                }


                // Gold
                $sql = "SELECT *
                                FROM UserStats us JOIN User u ON u.SteamID = us.SteamID
                                WHERE Rank >= ".GOLDELO." AND Rank < ".DIAMONDELO."
                                                ORDER BY Rank DESC
                                                LIMIT 1
                                                ";
                $ret['debug'] .= p($sql,1);
                $goldData = $DB->multiSelect($sql);
                if(count($goldData) > 0){
                        $ret['data']['gold'] = $goldData[0];
                }


                // Silver
                $sql = "SELECT *
                                FROM UserStats us JOIN User u ON u.SteamID = us.SteamID
                                WHERE Rank >= ".SILVERELO." AND Rank < ".GOLDELO."
                                                ORDER BY Rank DESC
                                                LIMIT 1
                                                ";
                $ret['debug'] .= p($sql,1);
                $silverData = $DB->multiSelect($sql);
                if(count($silverData) > 0){
                        $ret['data']['silver'] = $silverData[0];
                }


                // Bronze
                $sql = "SELECT *
                                FROM UserStats us JOIN User u ON u.SteamID = us.SteamID
                                WHERE Rank < ".SILVERELO."
                                                ORDER BY Rank DESC
                                                LIMIT 1
                                                ";
                $ret['debug'] .= p($sql,1);
                $bronzeData = $DB->multiSelect($sql);
                if(count($bronzeData) > 0){
                        $ret['data']['bronze'] = $bronzeData[0];
                }

                $ret['status'] = true;
                        
                return $ret;
        }

        
        function updateUserStatsElo($steamID, $eloChange, $result){
                $ret = array();
                $DB = new DB();
                $con = $DB->conDB();
                $ret['debug'] .= "Start updateUserStatsElo <br>\n";
                if($steamID > 0){
                	switch ($result) {
                		case "1":
                			$set = "Rank = Rank + ".(int)$eloChange;
                		break;
                		
                		case "-1":
                			$set = "Rank = Rank - ".(int)$eloChange;
                			break;
                		
                		default:
                			;
                		break;
                	}
                        // WON
//                         if($result == 1){
//                                 $set = "Rank = Rank + ".(int)$eloChange;
//                         }
//                         else{
//                                 $set = "Rank = Rank - ".(int)$eloChange;
//                         }
        
                        $sql = "UPDATE UserStats
                                        SET ".$set."
                                        WHERE SteamID = ".secureNumber($steamID)."
                                        ";
                        $ret['debug'] .= p($sql,1);
                        $DB->update($sql);
                        $ret['status'] = true;
                }
                else{
                        $ret['status'] = "SteamID = 0";
                }
        
                $ret['debug'] .= "End updateUserStatsElo <br>\n";
        
                return $ret;
        }
}

?>